/***********************************************
 This file is part of redia-bapps-main-ios.
 
 Copyright (c) 2015 Redia A/S
 
 Redia-bapps-main-ios is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.
 
 Redia-bapps-main-ios is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.
 
 You should have received a copy of the GNU General Public License
 along with redia-bapps-main-ios.  If not, see <http://www.gnu.org/licenses/>.
 
 *********************************************** */

//
//  BACustomNavigationBar.m
//  redia-bapps-ios
//
//  Created by Henrik Lynbech on 06/08/13.
//  Copyright (c) 2013 redia. All rights reserved.
//

#import "BACustomNavigationBar.h"
#import <QuartzCore/QuartzCore.h>
#import "BALibrarySettings.h"
#import "UIImage+MultiplyColor.h"
#import "BAUtils.h"


@implementation BACustomNavigationBar

-(void)setUpViewCustomNavigationBar
{
    //[self setBackgroundImage:[UIImage imageNamed:@"tabbar_bg"] forBarMetrics:UIBarMetricsDefault];
    //[navigationBar setNeedsDisplay];
    
    /*
    [self setClipsToBounds:NO];
    self.layer.shadowColor = [UIColor blackColor].CGColor;
    self.layer.shadowOpacity = 0.5;
    self.layer.shadowRadius = 1;
    self.layer.shadowOffset = CGSizeMake(0.0f, 1.0f);
    UIBezierPath *path = [UIBezierPath bezierPathWithRect:self.bounds];
    self.layer.shadowPath = path.CGPath;
    */

    UIColor* customcolor = BA_CUSTOM_NAVBAR_DEFAULT_GRAY_COLOR;
    
    [self setTintColor:customcolor]; // [[BALibrarySettings instance] getUIColorNormal]]; //deprecated?
    [self setBackgroundColor:customcolor]; // [[BALibrarySettings instance] getUIColorNormal]];
    
    if ([self respondsToSelector:@selector(setBarTintColor:)]) {
        [self setBarTintColor:customcolor]; // [[BALibrarySettings instance] getUIColorNormal]];
    }
    if ([self respondsToSelector:@selector(setTranslucent:)]) {
        [self setTranslucent:NO];
    }
    
    if ([[BAUtils instance] iOSMajorVersion] < 7) {
        self.shadowImage = [UIImage new];
        [self setBackgroundImage:[UIImage new] forBarMetrics:UIBarMetricsDefault];
        UIView* hline = [[UIView alloc] initWithFrame:CGRectMake(0, self.frame.size.height-1, 320, 1)];
        [hline setAutoresizingMask:(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin)];;
        [hline setBackgroundColor:[UIColor colorWithWhite:0 alpha:0.15]];
        [self addSubview:hline];
    }
    
    UIFont* titlefont = [UIFont fontWithName:@"Roboto-Regular" size:15.0f];
    UIColor* titlecolor = [UIColor whiteColor];
    self.titleTextAttributes = [NSDictionary dictionaryWithObjectsAndKeys:titlefont, UITextAttributeFont, titlecolor, UITextAttributeTextColor, nil];
    
    
    mainColorBar = [[UIView alloc] initWithFrame:CGRectMake(0,
                                                            BACUSTOMNAVBAR_TOPBARHEIGHT,
                                                            320,
                                                            self.frame.size.height - BACUSTOMNAVBAR_TOPBARHEIGHT)];
    [mainColorBar setAutoresizingMask:(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin)];;
    [mainColorBar setUserInteractionEnabled:NO];
    mainColorBar.alpha = 0;
    [self addSubview:mainColorBar];
    
    
    UIView* blackbar = [[UIView alloc] initWithFrame:CGRectMake(0, -20, 320, 20)];
    [blackbar setAutoresizingMask:(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin)];;
    blackbar.backgroundColor=[UIColor blackColor];
    [self addSubview:blackbar];
    
    topColorBar = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, BACUSTOMNAVBAR_TOPBARHEIGHT)];
    [topColorBar setAutoresizingMask:(UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin)];;
    [self addSubview:topColorBar];
    
    [self setBarColorWithCustomerColor];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(setBarColorWithCustomerColor) name:BA_LIBRARY_SETTINGS_OBJECT_CHANGED object:nil];
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:BA_LIBRARY_SETTINGS_OBJECT_CHANGED object:nil];
}

-(void)setBarColorWithCustomerColor
{
    [mainColorBar setBackgroundColor:[[BALibrarySettings instance] getCustomerColorNormal]]; //[UIColor colorWithWhite:228.0/255.0 alpha:1]];
    [topColorBar setBackgroundColor:[[BALibrarySettings instance] getCustomerColorNormal]];
    
}


- (void)layoutSubviews
{
    [super layoutSubviews];
    
    for (UIView *subView in self.subviews) {
        if ([subView isKindOfClass:[UIButton class]]) {
            CGFloat centerY = self.frame.size.height / 2 + (mainColorBar.alpha < 0.1 ? BACUSTOMNAVBAR_TOPBARHEIGHT / 2 : 0);
            CGPoint center = subView.center;
            center.y = centerY;
            subView.center = center;
        }
    }
}

-(id)init{
    self = [super init];
    if (self) {
        [self setUpViewCustomNavigationBar];
    }
    return self;
}

-(id)initWithCoder:(NSCoder *)aDecoder{
    self = [super initWithCoder:aDecoder];
    if (self) {
        [self setUpViewCustomNavigationBar];
    }
    return self;
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        [self setUpViewCustomNavigationBar];
    }
    return self;
}


-(void)changeToTopOnlyCustomerColorAnimated:(BOOL)animated
{
    //[mainColorBar setBackgroundColor:[UIColor colorWithWhite:228.0/255.0 alpha:1]];
    if (animated) {
        [UIView animateWithDuration:0.5
                         animations:^{
                             mainColorBar.alpha = 0.0;
                         }
         ];
    } else {
        mainColorBar.alpha=0.0;
    }
    //[mainColorBar setHidden:YES];
    
    //self.backIndicatorTransitionMaskImage = [UIImage imageNamed:@"backbutton-white.png" multiplyWithColor:[UIColor colorWithWhite:102.0/255.0 alpha:1]];

}

-(void)changeToFullCustomerColorAnimated:(BOOL)animated
{
    //[mainColorBar setBackgroundColor:[[BALibrarySettings instance] getUIColorNormal]];
    [self performSelector:@selector(delayedRestacking) withObject:nil afterDelay:0.1];
    if (animated) {
        [UIView animateWithDuration:0.5
                         animations:^{
                             mainColorBar.alpha = 1.0;
                         }
         ];

    } else {
        mainColorBar.alpha=1.0;
    }
    //[mainColorBar setHidden:NO];
    
    //self.backIndicatorTransitionMaskImage = [UIImage imageNamed:@"backbutton-white.png"];
}

-(void)delayedRestacking
{
    int index = 1;
    if ([mainColorBar.superview.subviews count]==0) {
        index = 0;
    }
    [mainColorBar.superview insertSubview:mainColorBar atIndex:index]; //ensure that this view is behind all buttons

}

@end
